package cn.coconet.clothingguide.repository;

import cn.coconet.clothingguide.model.Clothing;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

// 模拟商品数据库，提供查询功能
@Component
public class ClothingRepository {

    // 商品库存列表
    private List<Clothing> clothingList = new ArrayList<>();

    // 初始化模拟数据（项目启动时执行）
    @PostConstruct
    public void initData() {
        clothingList.add(new Clothing(
                "1001", "宽松纯棉T恤", "上衣", "休闲", "白色", 89.9, "M", "纯棉"
        ));
        clothingList.add(new Clothing(
                "1002", "高腰直筒牛仔裤", "裤子", "休闲", "蓝色", 159.0, "L", "牛仔布"
        ));
        clothingList.add(new Clothing(
                "1003", "真丝小黑裙", "裙子", "正式", "黑色", 329.0, "S", "真丝"
        ));
        clothingList.add(new Clothing(
                "1004", "速干运动上衣", "上衣", "运动", "灰色", 129.0, "M", "聚酯纤维"
        ));
        clothingList.add(new Clothing(
                "1005", "羊毛呢大衣", "外套", "正式", "驼色", 599.0, "L", "羊毛"
        ));
    }

    // 多条件查询商品
    public List<Clothing> findByFilters(
            String category,  // 类别（可为null，不筛选）
            String style,     // 风格（可为null）
            String color,     // 颜色（可为null）
            Double maxPrice   // 最高价格（可为null）
    ) {
        return clothingList.stream()
                .filter(c -> (category == null || c.getCategory().contains(category)))
                .filter(c -> (style == null || c.getStyle().contains(style)))
                .filter(c -> (color == null || c.getColor().contains(color)))
                .filter(c -> (maxPrice == null || c.getPrice() <= maxPrice))
                .collect(Collectors.toList());
    }
}